package com.example.demo.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.example.demo.dao.UserDao;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * user
 * @author 
 */
@Data
@Component
public class User implements Serializable {
    private Long userId;

    private String name;

    private int age;

    private String password;

//    private List<Dishes> viewedDishes = new ArrayList<>();

    private List<Dishes> dishes = new ArrayList<>();

    private String address;

    private String phone;

    private String avatar;

    private static final long serialVersionUID = 1L;

    // 修改：不再存储直接的 viewedDishes 列表，而是存储链表的节点列表
    private List<ViewedDishNode> viewedDishNodes = new ArrayList<>();

    @Autowired
    UserDao userDao;

    // 添加菜品到用户的观看链表中
    public void addViewedDish(Dishes dish) {
        User user = userDao.findByName(name);
        ViewedDishNode newNode = new ViewedDishNode(dish.getDishId());

        // 如果链表为空，则直接将新节点设置为头节点
        if (viewedDishNodes.isEmpty()) {
            viewedDishNodes.add(newNode);
        } else {
            // 否则，将新节点添加到链表的末尾
            ViewedDishNode lastNode = viewedDishNodes.get(viewedDishNodes.size() - 1);
            lastNode.setNextNode(newNode);
        }
    }


    public void shareDishWithUser(Dishes dish, User otherUser) {
        otherUser.addViewedDish(dish);
    }
}